;
(function (window, angular) {
    'use strict';
    angular.module("login.manager", ["ui.router", "ngSanitize", "ui.bootstrap", "angular.filter", "YB.Widget", "toggle-switch", "toaster", "Directive.All", "Service.All", "Filter.All", "template-directive", "template-widget"])
        .run(["$document", "$rootScope", "ybConfig", "$state", function ($document, $rootScope, ybConfig, $state) {
            ybConfig.initConfig();
            $state.go("login");
        }])
        .controller("LoginCtrl", ['$scope','EventBus','UserContextService', function ($scope,EventBus,UserContextService) {
            $scope.sysName = window.AppData.config.otherData.sysName;

            (function () {
                //getSiteInfo();
            })()
        }])

        .config(['$stateProvider', "$logProvider", function ($stateProvider, $logProvider) {
            $logProvider.debugEnabled(true);

            $stateProvider.state("login", {
                url: "/login",
                templateUrl: "view/login/login.html",
                controller: "LoginPageCtrl"
            });

            $stateProvider.state("register", {
                url: "/register",
                templateUrl: "view/login/register.html",
                controller: "RegisterCtrl"
            });
            /*
             $stateProvider.state("forgetPassword", {
             url: "/forgetPassword",
             templateUrl: "view/login/forgetPassword.html",
             controller: "ForgetPwdCtrl"
             });*/
        }])
        .controller("RegisterCtrl", ['$scope','$state',function ($scope,$state) {
            $scope.failRegister = false;
            $scope.showFormError = false;
            $scope.orgObj = {};
            $scope.resetMessage = function () {
                $scope.failRegister = false;
            };
            $scope.cancel = function () {
                $state.go('login');
            };
            $scope.doRegister = function (isValid) {
                if (!isValid) {
                    $scope.showFormError = true;
                    return;
                }
                $scope.showFormError = false;
            };
        }])
        .controller("LoginPageCtrl", ['$scope', '$window', "UserContextService", 'ybHttp', 'ybWidget', 'ybConfig','$state', function ($scope, $window, UserContextService, ybHttp, ybWidget, ybConfig,$state) {
            $scope.failLogin = false;
            $scope.showFormError = false;
            $scope.user = {"loginName": "", "password": "", code: ""};
            $scope.resetMessage = function () {
                $scope.failLogin = false;
            };
            $scope.codeImg = '';
            var authId = '';

            function getAuthCode() {
                var service = "auth";
                var reqUrl = "/auth/authInfo";
                ybHttp.Get(service, reqUrl).then(function (data) {
                    if(data.code != 0){
                        ybWidget.ShowMessage(data.message);
                        return;
                    }
                    authId = data.value.authId;
                    $scope.codeImg = ybConfig.getConfig().serviceName.auth + '/auth/kaptcha?authid=' + data.value.authId;
                })
            }

            $scope.login = function (isValid) {
                if (!isValid) {
                    $scope.showFormError = true;
                    return;
                }
                $scope.showFormError = false;
                $scope.user.authId = authId;
                var result = UserContextService.Login($scope.user, true);
                // result.then(function (data) {
                //     $scope.failLogin = false;
                //     $window.location.href = "main.html";
                // },
                // function (data) {
                //     $scope.failLogin = true;
                //     $scope.Message = data;
                //     $scope.user.Password = "";
                //     $scope.refresh();
                // });
                //弹窗提示
                result.then(function () {
                    $window.location.href = "main.html";
                },function (data) {
                    ybWidget.ShowMessage(data)
                    $scope.refresh();
                });
            };
            
            $scope.refresh = function () {
                getAuthCode();
            };

            (function () {
                getAuthCode();
            })();
        }])
/*        .controller("ForgetPwdCtrl", ['$scope', 'ecHttp', '$state', 'ecWidget', 'MESG_TYPE', "$timeout", function ($scope, ecHttp, $state, ecWidget, MESG_TYPE, $timeout) {
            $scope.submitted = false;
            $scope.stepInfo = [
                {
                    desc: "验证登录名"
                },
                {
                    desc: "修改密码"
                }
            ];
            $scope.forgetPwdModel = {
                LoginName: "",
                Phone: "",
                NewPassword: "",
                ConfirmPassword: "",
                SmsCode: ""
            };
            $scope.isNoLoginName = false;
            $scope.loginName = "";
            $scope.currentStep = 0;
            $scope.showed = true;
            $scope.timeNext = 0;
            $scope.goLogin = function () {
                $state.go("login");
            };
            $scope.next = function (isValid) {
                $scope.submitted = false;
                switch ($scope.currentStep) {
                    case 0:
                        if (!isValid) {
                            $scope.submitted = true;
                            return;
                        }
                        $scope.currentStep++;
                        break;
                    case 1:
                        if (!isValid) {
                            $scope.submitted = true;
                            return;
                        }
                        $state.go("login");
                        break;
                }
            };

            $scope.resetState = function () {
                $scope.isNoLoginName = false;
            }

            $scope.checkLoginName = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var param = {};
                param.LoginName = $scope.forgetPwdModel.LoginName;
                ecHttp.Get("Account/SoamCheckLoginName", param).then(function (data) {
                    if (data.Code !== 0) {
                        $scope.isNoLoginName = true;
                        return;
                    }
                    $scope.isNoLoginName = false;
                    $scope.forgetPwdModel.Phone = data.Value;
                    $scope.currentStep = 1;
                    $scope.submitted = false;
                });
            };
            $scope.getSmsCode = function () {
                ecHttp.Get("Account/SoamGetSmsCode", {"loginName": $scope.forgetPwdModel.LoginName}).then(function (data) {
                    if (data.Code !== 0) {
                        ecWidget.ShowMessage(data);
                        return;
                    }
                    $scope.timeNext = data.Value;
                    $scope.showed = false;
                    var updateClock = function () {
                        $timeout(function () {
                            --$scope.timeNext;
                            if ($scope.timeNext > 0) {
                                updateClock();
                            }
                        }, 1000);
                    };
                    updateClock();
                });
            };

            $scope.updatePwd = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                if ($scope.forgetPwdModel.NewPassword != $scope.forgetPwdModel.ConfirmPassword) {
                    ecWidget.ShowMessage("密码输入不一致，请重新输入！");
                    return;
                }
                ecHttp.Post("Account/SoamRetrievePassword", $scope.forgetPwdModel).then(function (data) {
                    if (data.Code !== 0) {
                        ecWidget.ShowMessage(data);
                        return;
                    }
                    ecWidget.ShowMessage("修改密码成功，请重新登录！", MESG_TYPE.Note).then(
                        function () {
                            $state.go("login");
                            return;
                        }
                    );
                });
            };
        }])*/
    ;
})(window, window.angular);